package com.flong.gui.notepad;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.filechooser.FileFilter;
/**
 * @author liangjilong
 * @Description:记事本
 */
@SuppressWarnings("all")
public class NotePad extends MouseAdapter implements ActionListener
{
	private JFrame frame;				//主窗体
	private JTextArea textArea;			//文档显示区
	private JPopupMenu popupMenu;		//右键弹出式菜单
	private String file = "";			//当前打开的文件名
	private String clipBoard = "";		//复制或剪切板内容
	private Color c_color;				//文档字体颜色
	private JLabel stateLabel;			//状态栏
	private boolean modifyed = false;	//文档是否被修改过
	

	
	/**
	 * 初始化窗体
	 */
	public NotePad()//
	{
		frame = new JFrame("文本编辑器v1.1");
		JMenuBar jmb = new JMenuBar();
		JMenu m_file = new JMenu("文件(F)");
		JMenu m_edit = new JMenu("编辑(E)");
		JMenu m_style = new JMenu("格式(O)");
		JMenu m_help = new JMenu("帮助(H)");
		
		m_file.setMnemonic('F');
		m_edit.setMnemonic('E');
		m_style.setMnemonic('O');
		m_help.setMnemonic('H');
		
		JMenuItem mi_new = new JMenuItem("新建(N)");
		JMenuItem mi_open = new JMenuItem("打开(O)");
		JMenuItem mi_save = new JMenuItem("保存(S)");
		JMenuItem mi_saveas = new JMenuItem("另存为(A)");
		JMenuItem mi_exit = new JMenuItem("退出(E)");
		
		mi_new.addActionListener(this);
		mi_new.setActionCommand("new");
		mi_open.addActionListener(this);
		mi_open.setActionCommand("open");
		mi_save.addActionListener(this);
		mi_save.setActionCommand("save");
		mi_saveas.addActionListener(this);
		mi_saveas.setActionCommand("saveas");
		mi_exit.addActionListener(this);
		mi_exit.setActionCommand("exit");
		
		mi_new.setMnemonic('N');
		mi_open.setMnemonic('O');
		mi_save.setMnemonic('S');
		mi_saveas.setMnemonic('A');
		mi_exit.setMnemonic('E');
		
		m_file.add(mi_new);
		m_file.add(mi_open);
		m_file.add(mi_save);
		m_file.add(mi_saveas);
		m_file.add(mi_exit);
		
		JMenuItem mi_copy = new JMenuItem("复制(C)");
		JMenuItem mi_cut = new JMenuItem("剪切(X)");
		JMenuItem mi_paste = new JMenuItem("粘贴(V)");
		
		mi_copy.addActionListener(this);
		mi_copy.setActionCommand("copy");
		mi_cut.addActionListener(this);
		mi_cut.setActionCommand("cut");
		mi_paste.addActionListener(this);
		mi_paste.setActionCommand("paste");
		
		mi_copy.setMnemonic('C');
		mi_cut.setMnemonic('X');
		mi_paste.setMnemonic('V');
		
		m_edit.add(mi_copy);
		m_edit.add(mi_cut);
		m_edit.add(mi_paste);
		
		JMenuItem mi_font = new JMenuItem("字体(F)");
		JMenuItem mi_color = new JMenuItem("颜色(C)");
		
		mi_font.addActionListener(this);
		mi_font.setActionCommand("font");
		mi_color.addActionListener(this);
		mi_color.setActionCommand("color");
		
		mi_font.setMnemonic('F');
		mi_color.setMnemonic('C');
		
		m_style.add(mi_font);
		m_style.add(mi_color);
		
		JMenuItem mi_about = new JMenuItem("关于(G)");
		mi_about.addActionListener(this);
		mi_about.setActionCommand("about");
		mi_about.setMnemonic('G');
		m_help.add(mi_about);
		
		jmb.add(m_file);
		jmb.add(m_edit);
		jmb.add(m_style);
		jmb.add(m_help);
		
		JToolBar jtb = new JToolBar();
		URL resource = this.getClass().getResource("/images/new.gif");
		JButton btn_new = new JButton(new ImageIcon(resource));
		resource = this.getClass().getResource("/images/open.gif");
		JButton btn_open = new JButton(new ImageIcon(resource));
		resource = this.getClass().getResource("/images/save.gif");
		JButton btn_save = new JButton(new ImageIcon(resource));
		resource = this.getClass().getResource("/images/copy.gif");
		JButton btn_copy = new JButton(new ImageIcon(resource));
		resource = this.getClass().getResource("/images/cut.gif");
		JButton btn_cut = new JButton(new ImageIcon(resource));
		resource = this.getClass().getResource("/images/paste.gif");
		JButton btn_paste = new JButton(new ImageIcon(resource));
		
		btn_new.setToolTipText("新建文件");
		btn_open.setToolTipText("打开文件");
		btn_save.setToolTipText("保存文件");
		btn_copy.setToolTipText("复制");
		btn_cut.setToolTipText("剪切");
		btn_paste.setToolTipText("粘贴");
		
		btn_new.addActionListener(this);
		btn_new.setActionCommand("new");
		btn_open.addActionListener(this);
		btn_open.setActionCommand("open");
		btn_save.addActionListener(this);
		btn_save.setActionCommand("save");
		btn_copy.addActionListener(this);
		btn_copy.setActionCommand("copy");
		btn_cut.addActionListener(this);
		btn_cut.setActionCommand("cut");
		btn_paste.addActionListener(this);
		btn_paste.setActionCommand("paste");
		
		jtb.add(btn_new);
		jtb.add(btn_open);
		jtb.add(btn_save);
		jtb.add(btn_copy);
		jtb.add(btn_cut);
		jtb.add(btn_paste);
		
		popupMenu = new JPopupMenu();
		JMenuItem jm_copy = new JMenuItem("复制");
		JMenuItem jm_cut = new JMenuItem("剪切");
		JMenuItem jm_paste = new JMenuItem("粘贴");
		jm_copy.addActionListener(this);
		jm_copy.setActionCommand("copy");
		jm_cut.addActionListener(this);
		jm_cut.setActionCommand("cut");
		jm_paste.addActionListener(this);
		jm_paste.setActionCommand("paste");
		popupMenu.add(jm_copy);
		popupMenu.add(jm_cut);
		popupMenu.add(jm_paste);
		
		textArea = new JTextArea();
		textArea.add(popupMenu);
		textArea.addMouseListener(this);
		textArea.addKeyListener(new KeyAdapter()
		{
			public void keyTyped(KeyEvent e)
			{
				stateLabel.setText("修改");
				modifyed = true;
			}
		});
		JScrollPane p = new JScrollPane(textArea);	
		stateLabel = new JLabel("就绪");
		
		frame.setJMenuBar(jmb);
		frame.add(jtb, BorderLayout.NORTH);
		frame.add(p, BorderLayout.CENTER);
		frame.add(stateLabel, BorderLayout.SOUTH);
		frame.setSize(500, 600);
		frame.setLocation(400, 300);
		frame.setResizable(true);//是否禁制窗体的缩放.设置为false窗体的放大功能不可用。
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);//显示窗体
		
		setTitleIOC(frame);
		
	}

	/**
	 * 事件处理
	 */
	public void actionPerformed(ActionEvent e)//事件处理
	{
		int positon = textArea.getSelectionStart();
		String s = e.getActionCommand();
		
		if (s.equals("new"))
		{
			textArea.setText("");
			frame.setTitle("文本编辑器-未命名");
			file = "";
			stateLabel.setText("新建文档");
		}
		else if (s.equals("open"))
		{
			if (modifyed == true)
			{
				int i = JOptionPane.showConfirmDialog(frame, "文档" + file + "内容已经修改\n是否保存？");
				if (i == 0)
				{
					this.saveFile();
					modifyed = false;
					this.loadFile();
				}
				else if (i == 1)
				{
					this.loadFile();
				}
			}
			else
			{	
				this.loadFile();
			}
		}
		else if (s.equals("save"))
		{
			if(file.equals(""))
			{
				this.saveFileAs();
			}
			else
			{
				this.saveFile();
			}
		}
		else if(s.equals("saveas"))
		{
			this.saveFileAs();
		}
		else if (s.equals("exit"))
		{
			if (modifyed == true)
			{
				int i = JOptionPane.showConfirmDialog(frame, "文档" + file + "内容已经修改\n是否保存？");
				if (i == 0)
				{
					this.saveFile();
					System.exit(0);
				}
				else if (i == 1)
				{
					System.exit(0);
				}
			}
			else
			{
				System.exit(0);
			}
		}
		else if(s.equals("copy"))
		{
			clipBoard = textArea.getSelectedText();
			textArea.setSelectionStart(textArea.getSelectionEnd());
		}
		else if(s.equals("cut"))
		{
			clipBoard = textArea.getSelectedText();
			delete();
		}
		else if(s.equals("paste"))
		{
			if (!(clipBoard == null || clipBoard.equals("")))
			{
				String content = textArea.getText();
				String alter = content.substring(0,positon) + clipBoard + content.substring(positon);
				textArea.setText(alter);
				modifyed = true;
				stateLabel.setText("修改");
			}
		}
		else if(s.equals("about"))
		{
			JOptionPane.showMessageDialog(frame, "简单的文本编辑器 V1.1\n作者：520starone@163.com", "关于", 1);
		}
		else if(s.equals("font"))
		{
			new FontDialog(this);
		}
		else if(s.equals("color"))
		{
			c_color = JColorChooser.showDialog(frame, "颜色选择对话框", Color.BLACK);
			textArea.setForeground(c_color);
		}
	}
	
	/**
	 * 鼠标按下事件
	 */
	public void mousePressed(MouseEvent e)
	{
		if(e.isPopupTrigger()){
			popupMenu.show(textArea, e.getX(), e.getY());	
		}	
	}

	/**
	 * 鼠标释放
	 */
	public void mouseReleased(MouseEvent e)
	{
		if(e.isPopupTrigger())
		{
			popupMenu.show(textArea, e.getX(), e.getY());	
		}	
	}
	/***
	 * 加载文件
	 */
	public void loadFile()//
	{
		JFileChooser chooser = new JFileChooser();
		chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		chooser.setFileFilter(new FileFilter()
		{
			public boolean accept(File f)
			{
				return f.isDirectory() || f.getName().endsWith(".txt");
			}
			public String getDescription()
			{
				return "文本文档(*.txt)";
			}
		});
		int result = chooser.showOpenDialog(frame);
		if (result == JFileChooser.APPROVE_OPTION)
		{
			
			try
			{
				textArea.setText("");
				File ff = chooser.getSelectedFile();
				frame.setTitle("文本文件-" + ff.getPath());
				file = new String(ff.getPath());
				FileInputStream in = new FileInputStream(ff);
				int size = in.available();
				byte[] b = new byte[size];
				in.read(b);
				textArea.setText(new String(b));
				in.close();
				stateLabel.setText("打开完成");
			}
			catch (FileNotFoundException e1)
			{
				e1.printStackTrace();
			}
			catch (IOException e2)
			{
				e2.printStackTrace();
			}
		}
	}
	/**
	 * 保存文件
	 */
	public void saveFile()//
	{
		try
		{
			FileOutputStream out = new FileOutputStream(file);
			String s = textArea.getText();
			s = s.replace("\n", "\r\n");
			out.write(s.getBytes());
			out.close();
			stateLabel.setText("保存成功");
			modifyed = false;
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	/**
	 * 另存为文件
	 */
	public void saveFileAs()//
	{
		JFileChooser chooser = new JFileChooser();
		chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		chooser.setFileFilter(new FileFilter()
		{
			public boolean accept(File f)
			{
				return f.isDirectory() || f.getName().endsWith(".txt");
			}
			public String getDescription()
			{
				return "文本文档(*.txt)";
			}
		});
		chooser.setDialogTitle("另存为");
		int result = chooser.showSaveDialog(frame);
		
		if (result == JFileChooser.APPROVE_OPTION)
		{
			
			try
			{
				File ff = chooser.getSelectedFile();
				frame.setTitle("文本文件-" + ff.getPath());
				file = new String(ff.getPath());
				ff.createNewFile();
				FileOutputStream out = new FileOutputStream(ff);
				String s = textArea.getText();
				s = s.replace("\n", "\r\n");
				out.write(s.getBytes());
				out.close();
				stateLabel.setText("另存为成功");
				modifyed = false;
			}
			catch (IOException e1)
			{
				e1.printStackTrace();
			}
		}
	}
	
	/**
	 * 设置字体
	 * @param font
	 */
	public void setFont(Font font)//
	{
		textArea.setFont(font);
	}
	
	public JFrame getFrame()//获取主窗体
	{
		
		return this.frame;
	}
	
	/**
	 * 删除文档中剪切的内容
	 */
	public void delete()
	{
		int start = textArea.getSelectionStart();
		int end = textArea.getSelectionEnd();
		String content = textArea.getText();
		String alter = content.substring(0,start) + content.substring(end);
		textArea.setText(alter); 	
	}
	/**
	 * 设置标题的logo的ICO图标
	 * @param JFrame
	 */
	public void setTitleIOC(JFrame JFrame){
		try {
			String src = "/images/facets-logo.png"; // 图片路径
			Image image = ImageIO.read(this.getClass().getResource(src));
			// 创建图片对象
			JFrame.setIconImage(image); // 设置图标
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	
	public static void main(String[] args)
	{
		new NotePad();
	}
}

